package com.medical.service.impl;

import com.medical.mapper.UsageRingMapper;
import com.medical.pojo.UsageRing;
import com.medical.pojo.UsageVO;
import com.medical.service.UsageRingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UsageRingServiceImpl implements UsageRingService {
    @Autowired
    private UsageRingMapper usageRingMapper;

    @Override
    public List<UsageRing> list(UsageVO usageVO) {
        return usageRingMapper.list(usageVO);
    }

    @Override
    public List<String> findCurrentUsage(UsageVO usageVO) {
        List<UsageRing> list = usageRingMapper.list(usageVO);
        List<String> currentUsage = list.stream().map(UsageRing::getCurrentUsage).collect(Collectors.toList());
        return currentUsage;
    }

    @Override
    public List<String> findPreviousUsage(UsageVO usageVO) {
        List<UsageRing> list = usageRingMapper.list(usageVO);
        List<String> previousUsage = list.stream().map(UsageRing::getPreviousUsage).collect(Collectors.toList());
        return previousUsage;
    }

    @Override
    public List<Integer> findRingResults(UsageVO usageVO) {
        List<String> currentUsage = findCurrentUsage(usageVO);
        List<String> previousUsage = findPreviousUsage(usageVO);
        List<Integer> results = calculateHuanBi(currentUsage, previousUsage);
        return results;
    }

    public static List<Integer> calculateHuanBi(List<String> currentUsage, List<String> previousUsage){
        List<Integer> results = new ArrayList<>();

        for (int i = 0; i < currentUsage.size(); i++) {
            int current = Integer.parseInt(currentUsage.get(i));
            int previous = Integer.parseInt(previousUsage.get(i));
            int ring = (int)(((double)(current - previous) / previous) * 100);
            results.add(ring);
        }
        return results;
    }
}
